package by.grsu.samusev.page.pages;


import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.RequestDao;
import by.grsu.samusev.entity.Request;


public class RequestEditPage extends WebPage {
		public RequestEditPage(final Request request) {
	super();
	

	final Form<Request> form = new Form<Request>("form", new CompoundPropertyModel<Request>(request));
	add(form);
	

	// add input fields to form
	form.add(new TextField<String>("status"));
	form.add(new DateTextField("date","DD.mm.yy HH:MM" ));
	form.add(new SubmitLink("save") {
		@Override
		public void onSubmit() {
			super.onSubmit();

			RequestDao requestDao = new RequestDao(SamusevWebApplication.DATA_XML_FOLDER);
			if ( request.getId() == null) {
				System.out.println("save="+ request);
				 requestDao.saveNew( request);
			} else {
				System.out.println("update="+ request);
				 requestDao.update( request);
			}

			setResponsePage(new RequestListPage());
		}
	});
	
	form.add(new Link("cancel") {
		@Override
		public void onClick() {
			setResponsePage(new RequestListPage());
		}
	});
	
}
}